const app = getApp()

import form_submit from '../../mixins/form_submit.js'
import { HiddenCall } from '../../request/api'
const request = new HiddenCall
import { loginExit } from "../../utils/http.js"
Page({
/**
  *  修改密码
  * - @author           王磊
  * - @date             2018-12-02
  * firstPassword     输入的新密码
  * lastPassword     再次输入的密码
  */
  mixins: [form_submit],
  data: {
    firstPassword: '',
    lastPassword: ''
  },
  onLoad (options) {
    this.setData({ jump: options.login ? options.login:''})
  },
  /**
   * 获取第一次输入的密码
   * @author wl
   */
  getFirstPassword(e) {
    this.setData({ firstPassword: e.detail.value})
  },
  /**
   * 失焦验证手机号
   * @author wl
   * @date 2019-3-12
   */
  testPassword (value) {
    let reg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/
    return reg.test(value)
  },
  /**
   * 输入新密码不够6位提示
   * @author wl
   */
  passWordPrompt () {
    if (this.data.firstPassword) this.setData({ isPassworld: this.testPassword(this.data.firstPassword)})
  },
  /**
   * 获取再次输入的密码
   * @author wl
   */
  getLastPassword(e) {
    this.setData({ lastPassword: e.detail.value })
    if (this.data.firstPassword===this.data.lastPassword) this.setData({ isLastPassword: true})
  },
  lastPassword () {
    if (!this.data.lastPassword) return
    if (this.data.firstPassword===this.data.lastPassword) {
      this.setData({ isLastPassword: true})
    } else {
      this.setData({ isLastPassword: false})
    }
  },
   /**
   * 保存密码
   * @author wl
   */
  savePassword() {
    let that = this
     // 密码大于等于6位
      if ((this.data.firstPassword === this.data.lastPassword) && this.data.isPassworld) {
          // 判断输入密码是否小于6位
        this.setData({ btnLoading: true})
        let modfiy = {
          newPassword: this.data.lastPassword,
          phone: "",
          userId: ""
        }
        request.modifyPassword({ newPassword: this.data.lastPassword}).then((res) => {
          wx.showToast({ 
            title: '密码已更新~', 
            icon: 'none',
            success () {
              that.setData({ btnLoading: false })
              !that.data.jump?
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
                }, 500) : setTimeout(() => {
                  wx.switchTab({
                    url: '../index/index'
                  })
                }, 500)
              // 调用退出接口进入首页
              // loginExit().then(() => {
              //   // 延时10000毫秒进行跳转
              //   // setTimeout(() => {
              //     // wx.redirectTo({
              //     //   url: '../login/index'
              //     // })

              //   //   this.setData({ btnLoading: false })
              //   // }, 1000)
              // })
            }
          })
        })
      }
    }
})